package com.example.demo.core.configurer;

import com.example.demo.core.config.RedissonConfig;
import com.example.demo.core.resolver.ApiNotNullResolver;
import com.example.demo.core.resolver.DefaultValueResolver;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.concurrent.Executor;

@Configuration
public class PrimaryConfigurer implements WebMvcConfigurer {

    private final RedissonConfig redissonConfig;

    @Autowired
    public PrimaryConfigurer(RedissonConfig redissonConfig) {
        this.redissonConfig = redissonConfig;
    }


    @Bean("taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setPoolSize(20);
        executor.setThreadNamePrefix("demo-task-executor-");
        return executor;
    }


    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setCodec(new JsonJacksonCodec());
        config.useSingleServer()
                .setConnectTimeout(redissonConfig.getConnectTime())
                .setAddress(redissonConfig.getAddress());
        return Redisson.create(config);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(defaultValueResolver());
    }



    @Bean
    public DefaultValueResolver defaultValueResolver(){
        return new DefaultValueResolver();
    }

    @Bean
    public ApiNotNullResolver apiNotNullResolver(){
        return new ApiNotNullResolver();
    }

}
